//
// Created by wujehy on 2020/7/30.
//

#include "Network.pb.h"
const std::string HexCode = "0123456789ABCDEF";

static std::string StringToHex(const std::string &data);
static std::string HexToString(const std::string &data);

std::string HexToString(const std::string &data)
{
    std::string result;
    for (int i = 0; i < data.length(); i += 2)
    {
        std::string byte = data.substr(i, 2);
        char chr = (char) strtol(byte.c_str(), NULL, 16);
        result.push_back(chr);
    }
    return result;
}


std::string StringToHex(const std::string &data , bool pass = false)
{
    std::string result ;
    uint8_t currTemp = 0;
    for (int i = 0; i < data.size();  ++i )
    {
        currTemp = data[i] & 0xff;
        result.push_back(HexCode[currTemp>>4]);
        result.push_back(HexCode[currTemp&0x0f]);
        if (pass)
        {
            result.push_back(' ');
        }
    }
    return result;
}

void  gen(std::string text  , int  id = 1);
void  gen(std::string text  , int  id )
{
    TranData data;
    data.set_data("setip127.0.0.1");
    data.set_data(text);
    data.set_id(id);

    std::string serial ;

    data.SerializePartialToString(&serial);

    std::cout <<"name : "<<text <<", code : "<<StringToHex(serial , true)<<std::endl;
}
int main()
{
    gen("close");
    gen("reload");
    gen("setip127.0.0.1");
    gen("setport1234");
    gen("stat");
//    TranData data;
////    data.set_data("setport1234");
//    data.set_data("setip127.0.0.1");
////    data.set_data("close");
////    data.set_data("tran");
////    data.set_data("reload");
//    data.set_data("stat");
//    // 08 04 12 0B 73 65 74 70 6F 72 74 31 32 33 34 // ip 1234
//// 08 04 12 06 72 65 6C 6F 61 64 // reload 4
//// 08 04 12 0E 73 65 74 69 70 31 32 37 2E 30 2E 30 2E 31 // ip
//// 08 04 12 04 73 74 61 74 //stat
//
//
//    std::string serial ;
//
//    data.SerializePartialToString(&serial);
//
//    std::cout <<"Data : "<<StringToHex(serial , true)<<std::endl;
}